<?php
/**
 *@作者:MissZhang
 *@邮箱:<787727147@qq.com>
 *@创建时间:2021/7/14 下午3:29
 *@说明:数据管理控制器
 */

namespace app\admin\controller;


use app\common\model\JifenOrder;
use app\common\model\OrderGoods;
use app\common\model\Users;
use think\facade\Db;
use think\facade\View;

class Report extends Base
{
    public function index(){
        $start_time=$this->begin;
        $end_time=$this->end;
        if ($start_time>$end_time){
            $this->error("开始时间不能大于结束时间");
        }
        $orders = Db::name("order")
            ->field(" COUNT(*) as tnum,sum(order_amount) as amount, FROM_UNIXTIME(add_time,'%Y-%m-%d') as gap")
            ->where(" add_time >$start_time and add_time < $end_time  and order_status >0")
            ->group('gap')
            ->select();
        foreach ($orders as $val){
            $crr[$val['gap']] = $val['tnum'];
            $drr[$val['gap']] = $val['amount'];
        }
        for($i=$start_time;$i<=$end_time;$i=$i+24*3600){
            $tmp_num = empty($crr[date('Y-m-d',$i)]) ? 0 : $crr[date('Y-m-d',$i)];
            $tmp_amount = empty($drr[date('Y-m-d',$i)]) ? 0 : $drr[date('Y-m-d',$i)];
            $tmp_sign = empty($tmp_num) ? 0 : round($tmp_amount/$tmp_num,2);
            $order_arr[] = $tmp_num;
            $amount_arr[] = $tmp_amount;
            $sign_arr[] = $tmp_sign;
            $date = date('Y-m-d',$i);
            $list[] = array('day'=>$date,'order_num'=>$tmp_num,'amount'=>$tmp_amount,'sign'=>$tmp_sign,'end'=>date('Y-m-d',$i+24*60*60));
            $day[] = $date;
        }
        if ($list){
            rsort($list);
        }
        //统计订单总额 实际的订单支付钱数总和
        $order_amount = Db::name("order")
            ->where(" add_time >$start_time and add_time < $end_time  and order_status >0")
            ->sum('order_amount');
        //统计订单总数 购物车里面一起付款的分开算
        $order_num = Db::name("order_goods")
            ->where(" pay_time >$start_time and pay_time < $end_time  and is_zhifu=1")
            ->count();
        $order_result = ['order'=>$order_arr,'amount'=>$amount_arr,'sign'=>$sign_arr,'time'=>$day];
        View::assign('order_result',json_encode($order_result));
        View::assign('list',$list);
        View::assign('order_amount',$order_amount);
        View::assign('order_num',$order_num);
        return view();
    }
    //销售明细
    public function sale_order(){
        $order=new \app\common\model\Order();
        $where[]=['order_status','>',0];
        $list=$order->where($where)->whereBetween('add_time',"$this->begin,$this->end")->paginate(
            [
                'list_rows'=>$this->page_size
            ]
        );
        // 获取分页显示
        $page = $list->render();
        View::assign('list',$list);
        View::assign('page', $page);
        return view();
    }
    //销售排行
    public function sale_top(){
        $order=new \app\common\model\OrderGoods();
        $where[]=['is_zhifu','=',1];
        $where[]=['uniacid','=',config('site.uniacid')];

        $list=$order->where($where)
            ->field("uniacid,goods_id,avg(goods_price*goods_num) as danjia,sum(goods_price*goods_num) as amount,goods_name,goods_sn,sum(goods_num) as goods_num")
            ->group('goods_id')
            ->order('amount desc')
            ->whereBetween('pay_time',"$this->begin,$this->end")
            ->paginate([
                'list_rows'=>$this->page_size
            ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list',$list);
        View::assign('page', $page);
        View::assign('p',input('page/d',1));
        View::assign('page_size',$this->page_size);
        return view();
    }
    //导出销售排行
    public function export_saleTop(){
        $order=new \app\common\model\OrderGoods();
        $where[]=['is_zhifu','=',1];
        $list=$order->where($where)
            ->field("goods_id,avg(goods_price*goods_num) as danjia,sum(goods_price*goods_num) as amount,goods_name,goods_sn,sum(goods_num) as goods_num")
            ->group('goods_id')
            ->order('amount desc')
            ->whereBetween('pay_time',"$this->begin,$this->end")
            ->select();
        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;">排行</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">商品名称</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">销售额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">销售数量</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">均价</td>';
        $strTable .= '</tr>';
        if($list){
            foreach($list as $k=>$val){
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.($k+1).'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['goods_name'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['amount'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['goods_num'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.round($val['danjia'],2).'</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .='</table>';
        unset($list);
        adminLog("导出销售排行");
        downloadExcel($strTable,'销售排行');
        exit();
    }
     //地区排行
    public function area_top(){
        $order=new \app\common\model\Order();
        $where[]=['order_status','>',0];
        $where[]=['type','=',1];
        $list=$order->where($where)
            ->field("pid,province,avg(order_amount) as danjia,sum(order_amount) as amount,count(*) as goods_num")
            ->group('pid')
            ->order('amount desc')
            ->select();
        View::assign('list',$list);
        return view();
    }
     //团队排行
    public function team_top(){
        $order=new Users();
        $where[]=['td_yeji','>',0];
        $list=$order->where($where)
            ->field("user_id,mobile,td_yeji,realname")
            ->order('td_yeji desc,user_id asc')
            ->paginate(
                [
                    'list_rows'=>$this->page_size
                ]
            );
        // 获取分页显示
        $page = $list->render();
        View::assign('list',$list);
        View::assign('page', $page);
        View::assign('p',input('page/d',1));
        View::assign('page_size',$this->page_size);
        return view();
    }
    //会员排行
    public function user_top(){
        $order=new \app\common\model\Order();
        $where[]=['order_status','>',0];
        $search_type = input('search_type');
        $search_key = input('search_key');
        if ($search_key){
            if ($search_type=='mobile'){
                $ids=Db::name('users')->whereLike('mobile',"%$search_key%")->column('user_id');
                $where[]=['user_id','in',$ids];
            }else{
                $ids=Db::name('users')->whereLike('realname',"%$search_key%")->column('user_id');
                $where[]=['user_id','in',$ids];
            }
        }
        $list=$order->where($where)
            ->field("sum(total_amount) as order_amount,count(*) as order_num,user_id")
            ->group('user_id')
            ->order('order_amount desc,order_num desc,user_id desc')
            ->whereBetween('pay_time',"$this->begin,$this->end")
            ->paginate(
            [
                'query'=>['search_type'=>$search_type,'search_key'=>$search_key],
                'list_rows'=>$this->page_size
            ]
            );
        // 获取分页显示
        $page = $list->render();
        View::assign('list',$list);
        View::assign('page', $page);
        View::assign('p',input('page/d',1));
        View::assign('page_size',$this->page_size);
        return view();
    }
    //导出会员排行
    public function export_userTop(){
        $order=new \app\common\model\Order();
        $where[]=['order_status','>',0];
        $search_type = input('search_type');
        $search_key = input('search_key');
        if ($search_key){
            if ($search_type=='mobile'){
                $ids=Db::name('users')->whereLike('mobile',"%$search_key%")->column('user_id');
                $where[]=['user_id','in',$ids];
            }else{
                $ids=Db::name('users')->whereLike('realname',"%$search_key%")->column('user_id');
                $where[]=['user_id','in',$ids];
            }
        }
        $list=$order->where($where)
            ->field("sum(total_amount) as order_amount,count(*) as order_num,user_id")
            ->group('user_id')
            ->order('order_amount desc,order_num desc,user_id desc')
            ->whereBetween('pay_time',"$this->begin,$this->end")
            ->select();
        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;">排行</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">会员姓名</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">会员手机</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">订单数量</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">购物金额</td>';
        $strTable .= '</tr>';
        if($list){
            foreach($list as $k=>$val){
                $realname=Db::name('users')->where('user_id',$val['user_id'])->value('realname');
                if(empty($realname)){
                    $realname='---';
                }
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.($k+1).'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$realname.'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.getMobile($val['user_id']).'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['order_num'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['order_amount'].'</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .='</table>';
        unset($list);
        adminLog("导出会员排行");
        downloadExcel($strTable,'会员排行');
        exit();
    }
    //会员订单
    public function user_order(){
        $order=new \app\common\model\Order();
        $user_id=input('user_id');
        if ($user_id){
            $where[]=['user_id','=',$user_id];
        }
        $where[]=['order_status','>',0];
        $list=$order->where($where)
            ->order('add_time desc,order_id desc')
            ->whereBetween('add_time',"$this->begin,$this->end")
            ->paginate(
                [
                    'query'=>['user_id'=>$user_id],
                    'list_rows'=>$this->page_size
                ]
            );
        // 获取分页显示
        $page = $list->render();
        View::assign('list',$list);
        View::assign('page', $page);
        return view();
    }
    //销售明细
    public function sale_list(){
        $order=new \app\common\model\OrderGoods();
        $where[]=['is_zhifu','=',1];
        $goods_id=input('goods_id');
        if ($goods_id){
            $where[]=['goods_id','=',$goods_id];
        }
        $list=$order->where($where)
            ->order('pay_time desc')
            ->whereBetween('pay_time',"$this->begin,$this->end")
            ->paginate(
                [
                    'query'=>['goods_id'=>$goods_id],
                    'list_rows'=>$this->page_size
                ]
            );
        // 获取分页显示
        $page = $list->render();
        View::assign('list',$list);
        View::assign('page', $page);
        return view();
    }
    //导出销售明细
    public function export_saleList(){
        $order=new \app\common\model\OrderGoods();
        $where[]=['is_zhifu','=',1];
        $goods_id=input('goods_id');
        if ($goods_id){
            $where[]=['goods_id','=',$goods_id];
        }
        $list=$order->where($where)
            ->order('pay_time desc')
            ->whereBetween('pay_time',"$this->begin,$this->end")
            ->select();
        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;">商品ID</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">商品名称</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">商品价格</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">商品数量</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">出售日期</td>';
        $strTable .= '</tr>';
        if($list){
            foreach($list as $k=>$val){
                $realname=Db::name('users')->where('user_id',$val['user_id'])->value('realname');
                if(empty($realname)){
                    $realname='---';
                }
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['goods_id'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['goods_name'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['goods_price'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['goods_num'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.date('Y-m-d H:i:s',$val['pay_time']).'</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .='</table>';
        unset($list);
        adminLog("导出销售明细");
        downloadExcel($strTable,'销售明细');
        exit();
    }
    //会员统计
    public function user(){
        $users=Db::name('users')
            ->group('reg_time1')
            ->field("count(*) as num,FROM_UNIXTIME(reg_time,'%Y-%m-%d') as reg_time1")
            ->select();
        foreach ($users as $val){
            $arr[$val['reg_time1']] = $val['num'];
        }
        $begin = date('Y-m-d', strtotime("-3 month"));//3个月前
        $end = date('Y-m-d', strtotime('+1 days'));
        $start_time=strtotime($begin);
        $end_time=strtotime($end);
        for($i=$start_time;$i<=$end_time;$i=$i+24*3600){
            $brr[] = empty($arr[date('Y-m-d',$i)]) ? 0 : $arr[date('Y-m-d',$i)];
            $day1[] = date('Y-m-d',$i);
        }
        $result = array('data'=>$brr,'time'=>$day1);
        View::assign('result',json_encode($result));
        return view();
    }
}